<?php
// +----------------------------------------------------------------------
// | Workman and Gateway-worker
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2025 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace yicmf\worker\handler;


use think\facade\Log;
use Workerman\Timer;
use Workerman\Worker;
use yicmf\worker\Application;
use GatewayWorker\Lib\Gateway as LibGateway;

class Gateway
{

    /**
     * onConnect 事件回调
     * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
     * @access public
     * @param int $client_id
     * @return void
     */
    public static function onConnect($client_id)
    {
        LibGateway::sendToCurrentClient(self::_formatData('conn11ect ok', 'init'));
        //:todo 通知好友上线
    }


    /**
     * onMessage 事件回调
     * 当客户端发来数据(Gateway进程收到数据)后触发
     * @access public
     * @param int $client_id
     * @param mixed $data
     * @return void
     */
    public static function onMessage($client_id, $data)
    {
        LibGateway::sendToCurrentClient(self::_formatData($data, 'mssage'));
    }

    /**
     * onClose 事件回调 当用户断开连接时触发的方法
     * @param integer $client_id 断开连接的客户端client_id
     * @return void
     */
    public static function onClose($client_id)
    {
        // :TODO 通知用户离线
        LibGateway::sendToAll("client[$client_id] logout\n");
    }

    /**
     * onWorkerStart 事件回调
     * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
     * @access public
     * @return void
     */
    public static function onWorkerStart(Worker $worker)
    {
        $app = new Application();
        $app->initialize();
        // 0.1秒一次轮询
        Timer::add(0.1, function () use ($worker) {
            $time_now = time();
        });
    }


    /**
     * onWebSocketConnect 事件回调
     * 当客户端连接上gateway完成websocket握手时触发
     * @param integer $client_id 断开连接的客户端client_id
     * @param mixed $data
     * @return void
     */
    public static function onWebSocketConnect($client_id, $data)
    {
        echo "WorkerStop$client_id\n";
//        Log::channel('socket')->error('msg ' . $msg . ' error ' . $code);
    }

    /**
     * onWorkerStop 事件回调
     * 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
     * @param \Workerman\Worker $businessWorker
     * @return void
     */
    public static function onWorkerStop(Worker $businessWorker)
    {
        echo "WorkerStop\n";
//			Log::channel('socket')->error('msg ' . $msg . ' error ' . $code);
    }


    public function onWorkerReload($worker)
    {
        // 重启服务
        echo "onWorkerReload\n";
    }

    /**
     * 数据格式化
     * @param               $data
     * @param  $type
     * @return string
     * @author 微尘 <yicmf@qq.com>
     */
    private static function _formatData($data, $type): string
    {
        $_data = [
            'time' => time(),
            'type' => $type,
            'data' => $data
        ];
        Log::error('send ' . json_encode($_data));
        return json_encode($_data);
    }


    private static function dataCheck($data)
    {
        json_decode($data);
        return (json_last_error() == JSON_ERROR_NONE) && !is_numeric($data);
    }
}